function el(e) {
	return document.getElementById(e);
}

/* Utilizado criar uma tela popup no browser e centralizar na tela do usuario. */
function novaJanela(mypage,myname,w,h,scroll,pos){
    var LeftPosition;
    var TopPosition;
    if (pos=="random") {
        LeftPosition=(window.screen.width)?Math.floor(Math.random()*(
        window.screen.width-w)):100;
        TopPosition=(window.screen.height)?Math.floor(Math.random()*(
        (window.screen.height-h))):100;
    } else
        if (pos=="tela") {
            LeftPosition=0
            TopPosition=0;
        } else
            if (pos=="center") {
                LeftPosition=(window.screen.width)?(window.screen.width-w)/2:100;
                TopPosition=(window.screen.height)?((window.screen.height-h)-200)/2:100;
            } else 
                if ((pos!="center" && pos!="random") || pos===null) {
                    LeftPosition=0;
                    TopPosition=20;
                }
    var settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=yes,menubar=no,toolbar=no,resizable=yes';
    return window.open(mypage,myname,settings);
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

function numero(v){
    return v.replace(/\D/g,"")
}

function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que n�o � d�gito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca par�nteses em volta dos dois primeiros d�gitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca h�fen entre o quarto e o quinto d�gitos
    return v
}

function cpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que n�o � d�gito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto d�gitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto d�gitos
    //de novo (para o segundo bloco de n�meros)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um h�fen entre o terceiro e o quarto d�gitos
    return v
}

function titulo(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que n�o � d�gito
    v=v.replace(/^(\d{4})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro d�gitos
    v=v.replace(/^(\d{4})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto d�gitos
    v=v.replace(/\.(\d{3})(\d)/,".$1-$2")           //Coloca uma barra entre o oitavo e o nono d�gitos
	return v
}

function cep(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que n�o � d�gito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse � t�o f�cil que n�o merece explica��es
    return v
}

function cnpj(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que n�o � d�gito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro d�gitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto d�gitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono d�gitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um h�fen depois do bloco de quatro d�gitos
    return v
}

// tem que ser na onkeypress
function maiusculo(v){
    v=v.toUpperCase()           
    return v
}

function minusculo(v){
    v=v.toLowerCase()           
    return v
}

function romanos(v){
    v=v.toUpperCase()             //Mai�sculas
    v=v.replace(/[^IVXLCDM]/g,"") //Remove tudo o que n�o for I, V, X, L, C, D ou M
    //Essa � complicada! Copiei daqui: http://www.diveintopython.org/refactoring/refactoring.html
    while(v.replace(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/,"")!="")
        v=v.replace(/.$/,"")
        return v
}

function site(v){
    //Esse sem comentarios para que você entenda sozinho ;-)
    v=v.replace(/^www:\/\/?/,"")
    dominio=v
    caminho=""
    if(v.indexOf("")>-1)
        dominio=v.split("/")[0]
        caminho=v.replace(/[^\/]*/,"")
        dominio=dominio.replace(/[^\w\.\+-:@]/g,"")
        caminho=caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g,"")
        caminho=caminho.replace(/([\?&])=/,"$1")
        if(caminho!="")dominio=dominio.replace(/\.+$/,"")
        v="www://"+dominio+caminho
        return v
}

function data(v){
    v=v.replace(/\D/g,"") //Remove tudo o que n�o � d�gito
    v=v.replace(/(\d{2})(\d)/,"$1/$2") //Coloca barra entre o segundo e o terceiro digito 
    v=v.replace(/(\d{2})(\d)/,"$1/$2") //Coloca barra entre o quinto e o sexto digito
    return v
}

function hora(v){
	v=v.replace(/\D/g,"") //Remove tudo o que n�o � d�gito
	v=v.replace(/(\d{2})(\d)/,"$1:$2") // Coloca dois pontos entre o segundo e o terceiro d�gito
	return v
}	


function valor(v) {
    v=v.replace(/\D/g,"") //Remove tudo o que n�o � d�gito 
    if (v.length < 6) { // ###,##
        var exp =  /(\d)(\d{2})$/;
        var pd = "$1,$2";
        v=v.replace(exp,pd); 
        return v;
    }
    if (v.length >= 6 && v.length < 9) { // ###.###,##
        var exp =  /(\d{1})(\d{3})(\d{2})$/;
        var pd = "$1.$2,$3";
        v=v.replace(exp,pd); 
        return v;
    } 
    if (v.length >= 9 && v.length < 12) { // ###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3,$4";
        v=v.replace(exp,pd);
        return v;
    }
    if (v.length >= 12 && v.length < 15) { // ###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4,$5";
        v=v.replace(exp,pd);
        return v;
    }
    if (v.length >= 15 && v.length < 18) { // ###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5,$6";
        v=v.replace(exp,pd);
        return v;
    }
    if (v.length >= 18 && v.length < 21) { // ###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6,$7";
        v=v.replace(exp,pd);
        return v;
    }
    if (v.length >= 21 && v.length < 24) { // ###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7,$8";
        v=v.replace(exp,pd);
        return v;
    }
    if (v.length >= 24 && v.length < 27) { // ###.###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7.$8,$9";
        v=v.replace(exp,pd);
        return v;
    } 
    if (v.length >= 27) { // ###.###.###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7.$8.$9,$10";
        v=v.replace(exp,pd);
        return v;
    } 
}

function valorCifrao(v) {
    v=v.replace(/\D/g,"") //Remove tudo o que n�o � d�gito
    if (v == null || v == "R$ " || v == "") {
        return "";
    }
    if (v.length < 6) { // ###,##
        var exp =  /(\d)(\d{2})$/;
        var pd = "$1,$2";
        v=v.replace(exp,pd); 
        return "R$ "+v;
    }
    if (v.length >= 6 && v.length < 9) { // ###.###,##
        var exp =  /(\d{1})(\d{3})(\d{2})$/;
        var pd = "$1.$2,$3";
        v=v.replace(exp,pd); 
        return "R$ "+v;
    } 
    if (v.length >= 9 && v.length < 12) { // ###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3,$4";
        v=v.replace(exp,pd);
        return "R$ "+v;
    }
    if (v.length >= 12 && v.length < 15) { // ###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4,$5";
        v=v.replace(exp,pd);
        return "R$ "+v;
    }
    if (v.length >= 15 && v.length < 18) { // ###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5,$6";
        v=v.replace(exp,pd);
        return "R$ "+v;
    }
    if (v.length >= 18 && v.length < 21) { // ###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6,$7";
        v=v.replace(exp,pd);
        return "R$ "+v;
    }
    if (v.length >= 21 && v.length < 24) { // ###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7,$8";
        v=v.replace(exp,pd);
        return "R$ "+v;
    }
    if (v.length >= 24 && v.length < 27) { // ###.###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7.$8,$9";
        v=v.replace(exp,pd);
        return "R$ "+v;
    } 
    if (v.length >= 27) { // ###.###.###.###.###.###.###.###.###,##
        var exp =  /(\d{1})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{3})(\d{2})$/;
        var pd = "$1.$2.$3.$4.$5.$6.$7.$8.$9,$10";
        v=v.replace(exp,pd);
        return "R$ "+v;
    } 
}

/** Tratamento de erros genericos com DWR. **/
function callBackException(msg, exception) {
	if (exception.erros != null) {
	    var mess = '';
	    for (var ct=0; ct < exception.erros.length; ++ct) {
	        mess += exception.erros[ct]+'\n';
	    }
	    window.alert(mess);
	} else {
	    window.alert(msg+":\n Verifique o log do web container.");
	}
}